﻿using Styx;
using Styx.Common;
using Styx.Common.Helpers;
using Styx.CommonBot;
using Styx.CommonBot.Frames;
using Styx.CommonBot.Inventory;
using Styx.CommonBot.Profiles;
using Styx.Helpers;
using Styx.Pathing;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Media;
using System.Xml.Linq;

namespace ClothShufflePlugin
{
    public class ClothShufflePlugin : HBPlugin
    {
        public override string Name { get { return "Celisuis Cloth Profile Plugin"; } }
        public override string Author { get { return "Celisuis"; } }
        public override Version Version { get { return new Version(2, 0); } }
        public static LocalPlayer Me = StyxWoW.Me;
        public override bool WantButton { get { return true; } }
        public override string ButtonText { get { return "Disenchant Items"; } }
        private Thread deThread = null;


        public static uint[] chestIDs = new uint[] { 67539, 87391 };
        public static uint[] cataPot = new uint[] { 58488 };
        public static uint[] pandaPot = new uint[] { 93351 };

        public static uint[] cataZones = new uint[] {720, 606, 708, 709, 700 };
        public static uint[] pandaZones = new uint[] { 858, 929, 928, 857, 809, 905, 903, 806, 873, 808, 951, 810, 811, 807 };

         private static List<WoWItem> alreadyDisenchanted = new List<WoWItem>();
    
        private List<uint> ignoreItems = new List<uint>() {
            // mats
            10940, 10938, 10939, 10998, 10978, 11082, 11083, 11084, 11134, 11139, 11174, 11175,
            11176, 11177, 11178, 14343, 14344, 16202, 16203, 16204, 20725, 22445, 22446, 22447,
            22448, 22449, 22450, 34052, 34053, 34054, 34055, 34056, 34057, 46849, 49649, 52718,
			52719, 74250,

            // rods
            44452,

            // lockboxes
            4632, 4633, 4634, 4636, 4637, 4638, 5758, 5759, 5760, 31952, 43622, 68729, 88567,

            // healthstones
            5509, 5510, 5511, 5512, 9421, 19004, 19005, 19006,  
            19007, 19008, 19009, 19010, 19011, 19012, 19013, 
            22103, 22104, 22105, 36889, 36890, 36891, 36892,
            36893, 36894,

            // soulstones
			5232, 16892, 16893, 16895, 16896, 22116, 36895,

            // firestones
			40773, 41169, 41170, 41171, 41172, 41173, 41174,

            // spellstones
            41191, 41192, 41193, 41194, 41195, 41196,
			
			// archaeology keystones
			52843, 63127, 63128, 64392, 64394, 64395, 64396, 64397, 79868, 79869,
        };


        public static int delay = 2;
        public DateTime cur = DateTime.Now.AddSeconds(delay);

        public override void OnButtonPress()
        {
            for (int a = alreadyDisenchanted.Count - 1; a >= 0; a--)
            {
                alreadyDisenchanted.RemoveAt(a);
            }
            if(!SpellManager.HasSpell("Disenchant"))
            {
                Logging.Write(LogLevel.Normal, Colors.Green, "This Character does not have Enchanting.");
                return;
            }
            DisenchantItems();
        }   
        public override void Pulse()
        {
            //Some verification
            if (Me == null || !StyxWoW.IsInGame || Me.IsDead || Me.IsGhost || Battlegrounds.IsInsideBattleground || Me.IsOnTransport)
                return;
            //Not casting or in combat
            if (Me.IsCasting || Me.IsActuallyInCombat)
                return;

            // 2 sec delay, for a smooth process
            #region Farming
            if (DateTime.Now > cur)
            {
                var chest = Me.BagItems.Where(x => chestIDs.Contains(x.Entry) && x.Cooldown == 0);
                if (chest.Count() > 0)
                {
                    chest.First().Use();
                    Logging.Write("Using Treasure Chest");
                }
                cur = DateTime.Now.AddSeconds(delay);

                

            }

            if (!Me.HasAura("Potion of Treasure Finding"))// && ((Me.MapId == 720) || (Me.MapId == 606) || (Me.MapId == 708) || (Me.MapId == 709) || (Me.MapId == 700)))
            {
                var pot = Me.BagItems.Where(x => cataPot.Contains(x.Entry) && x.Cooldown == 0);
                {
                    if (pot.Count() > 0)
                        pot.First().Use();
                    Logging.Write("Treasure Finding Buff Expired. Renewing");
                }
            }

            if(!Me.HasAura("Potion of Luck"))// && ((Me.MapId == 858) || (Me.MapId == 929) || (Me.MapId == 928) || (Me.MapId == 857) || (Me.MapId == 809) || (Me.MapId == 806) || (Me.MapId == 873) || (Me.MapId == 808) || (Me.MapId == 951) || (Me.MapId == 810) || (Me.MapId == 811) || (Me.MapId == 807)))
            {
                var pot = Me.BagItems.Where(x => pandaPot.Contains(x.Entry) && x.Cooldown == 0);
                {
                    if (pot.Count() > 0)
                        pot.First().Use();
                    Logging.Write("Treasure Finding Buff Expired. Renewing");
                }

            }


            for (int a = alreadyDisenchanted.Count - 1; a >= 0; a--)
            {
                alreadyDisenchanted.RemoveAt(a);
            }
      
        }
#endregion
            #region Disenchanting
            private void DisenchantItems()
            {
                  List<WoWItem> targetItems = ObjectManager.GetObjectsOfType<WoWItem>(false);
			
            for (int a = targetItems.Count-1; a >= 0; a--)
            {
                    if (ignoreItems.Contains(targetItems[a].Entry) || alreadyDisenchanted.Contains(targetItems[a]))
                    {
                        targetItems.RemoveAt(a);
                    }
                    else if 
						(targetItems[a].IsSoulbound || 
							targetItems[a].IsAccountBound || 
								ignoreItems.Contains(targetItems[a].Entry) || 
									targetItems[a].Quality == WoWItemQuality.Poor || 
										targetItems[a].Quality == WoWItemQuality.Common ||
											targetItems[a].Quality == WoWItemQuality.Rare ||
												targetItems[a].Quality == WoWItemQuality.Epic)
					{
						alreadyDisenchanted.Add(targetItems[a]);
						targetItems.RemoveAt(a);
					}
            }

                if (Equals(null, targetItems)) { return; }

                foreach (WoWItem deItem in targetItems)
                {
					if(deItem.BagSlot != -1)
					{
						Logging.Write(LogLevel.Normal, Colors.DarkRed, "[LiquidDisenchant]: {0} (Entry:{1}).", deItem.Name, deItem.Entry);
						Lua.DoString("CastSpellByName(\"Disenchant\")");
						StyxWoW.SleepForLagDuration();
						Lua.DoString("UseItemByName(\"" + deItem.Name + "\")");
						StyxWoW.SleepForLagDuration();

                    while (StyxWoW.Me.IsCasting)
						{
							Thread.Sleep(250);
						}

                    Thread.Sleep(2500);

                    Stopwatch timer = new Stopwatch();
                    timer.Start();
					
						if (!StyxWoW.Me.IsActuallyInCombat)
						{
							Thread.Sleep(1500);
							alreadyDisenchanted.Add(deItem);
						}
					}
                }
            return;
            }
            #endregion




        }
    }
